package com.china08.yxyapi.api.manage.edu.platform.news;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.cache.YxCacheUtil;
import com.china08.yxyapi.entity.mg.platform.YxyHomeNews;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyHomeNewsRepository;
import com.china08.yxyapi.util.HtmlCompressorUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 新闻管理 新增
 * 
 */
@CrossOrigin
@RestController
public class NewsCrtController extends YxyApiBaseController {
    @Autowired
    private YxyHomeNewsRepository yhnr;

    @ApiOperation(value = "教育局 增加咨询")
    @RequestMapping(value = "/api_yw/manage/edu/platform/news/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel model = new RespModel();
        String eduId = getProfile(request, response).getSchoolId();
        String userId = getProfile(request, response).getId();
        YxyHomeNews homeNews = new YxyHomeNews();
        homeNews.setEdbId(eduId);
        reqModel.content = HtmlCompressorUtil.HtmlCompressor(reqModel.content);
        Assert.isTrue(StringUtils.length(Jsoup.parse(reqModel.content).text()) <= 20000, "内容禁止超过2万字");
        homeNews.setContent(reqModel.content);
        homeNews.setTitle(reqModel.title);
        homeNews.setCover(reqModel.cover);
        homeNews.setStatus(1);
        homeNews.setCreatedBy(userId);
        homeNews.setLastModifiedBy(userId);
        yhnr.save(homeNews);
        model.setMessage("成功");
        YxCacheUtil.eviceCacheAll();
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String title;
        public String cover;
        public String content;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(title, null), "请填写标题");
            Assert.notNull(StringUtils.defaultIfBlank(content, null), "请填写内容");
            Assert.notNull(StringUtils.defaultIfBlank(cover, null), "封面不能为空");
            Assert.isTrue(StringUtils.length(title) >= 2 && StringUtils.length(title) <= 36, "标题字数限制为2-36字");
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }

}
